/**
 *  Copyright(C) 2007 Juri Strumpflohner
 *  All rights reserved
 *
 *  Created: 17.07.2007
*/

package com.aroundj2me.sandbox;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.aroundj2me.utility.ProgressForm;

public class ProgressTest extends MIDlet implements CommandListener, Runnable {
	private Display display;
	private ProgressForm f;
	
	private List dummy = new List("navigation", List.IMPLICIT);
	
	public Command startProgress;
	public Command endProgress;
	public Command increment;
	
	boolean run = false;

	public ProgressTest() {
		this.f = new ProgressForm(true);
		startProgress = new Command("Start progress", Command.SCREEN, 1);
		endProgress = new Command("Stop progress", Command.SCREEN, 1);
		increment = new Command("increment", Command.SCREEN,1);
		dummy.addCommand(startProgress);
		dummy.addCommand(endProgress);
		dummy.addCommand(increment);
		dummy.setCommandListener(this);
		
//		this.f.initialize(4);
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
	}

	protected void pauseApp() {
	}

	protected void startApp() throws MIDletStateChangeException {
		display = Display.getDisplay(this);
		display.setCurrent(dummy);
	}
	
	
	public void commandAction(Command c, Displayable d) {
		if(c == startProgress){
			this.display.setCurrent(f);
			f.startProgress("Loading places...");
//			this.run = true;
//			Thread t = new Thread(this);
//			t.start();
		}else if(c == endProgress){
			this.display.setCurrent(dummy);
			f.stopProgress();
			this.run = false;
		}
	}

	public void run() {
		int i=0;
		while(run){
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.f.incrementProgress();
			System.out.println("incremented (" + i + ")");
			i++;
		}
		
	}

}